home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 43 / Amiga Format CD43 (1999)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-09].iso / -serious- / programming / other / python-1.52 / demo / scripts / from.py < prev    next >
Text File  |  1999-06-14  |  823b  |  36 lines

  1. #! /usr/bin/env python
  2.  
  3. # Print From and Subject of messages in $MAIL.
  4. # Extension to multiple mailboxes and other bells & whistles are left
  5. # as exercises for the reader.
  6.  
  7. import sys, os
  8.  
  9. # Open mailbox file.  Exits with exception when this fails.
  10.  
  11. try:
  12.     mailbox = os.environ['MAIL']
  13. except (AttributeError, KeyError):
  14.     sys.stderr.write('No environment variable $MAIL\n')
  15.     sys.exit(2)
  16.  
  17. try:
  18.     mail = open(mailbox, 'r')
  19. except IOError:
  20.     sys.stderr.write('Cannot open mailbox file: ' + mailbox + '\n')
  21.     sys.exit(2)
  22.  
  23. while 1:
  24.     line = mail.readline()
  25.     if not line: break # EOF
  26.     if line[:5] == 'From ':
  27.         # Start of message found
  28.         print line[:-1],
  29.         while 1:
  30.             line = mail.readline()
  31.             if not line: break # EOF
  32.             if line == '\n': break # Blank line ends headers
  33.             if line[:8] == 'Subject:':
  34.                 print `line[9:-1]`,
  35.         print
  36.